package com.example.lastwork.service;


import com.example.lastwork.common.service.LogicService;
import com.example.lastwork.dao.BusinessDao;
import com.example.lastwork.dao.ItemDao;
import com.example.lastwork.dao.ShopDao;
import com.example.lastwork.dao.ShopItemDao;
import com.example.lastwork.entity.Business;
import com.example.lastwork.entity.Item;
import com.example.lastwork.entity.Shop;
import com.example.lastwork.entity.ShopItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BusinessService extends LogicService<BusinessDao, Business, Long> {
    @Resource
    private ShopDao shopDao;

    @Resource
    private ItemDao itemDao;

    @Resource
    private ShopItemDao shopItemDao;

    public BusinessService(@Autowired BusinessDao dao){
        super(dao);
    }

    public Shop createShop(Long businessId, String shopName){
        Business business = getDAO().getReferenceById(businessId);
        Shop shop = business.createShop(shopName);
        shopDao.save(shop);
        return shop;
    }

    public String createItem(Long businessId, String shopItemName, Double price,Long shopId,Long itemId){
        Business business = getDAO().getReferenceById(businessId);
        Item item = itemDao.getReferenceById(itemId);
        Shop shop = shopDao.getReferenceById(shopId);
        ShopItem shopItem = business.createShopItem(shopItemName, price, shop, item);
        shopItemDao.save(shopItem);
        return "添加成功";
    }

    @Transactional
    public String confirmOrder(Long ordersId){
        int result = getDAO().confirmOrder(ordersId);
        if (result == 1)
            return "确认订单成功";
        return "确认订单失败";
    }

    @Transactional
    public String refundOrder(Long ordersId){
        int result = getDAO().refundOrder(ordersId);
        if (result == 1)
            return "订单退款成功";
        return "订单退款失败";
    }
}
